<template>
  <div>
    <!-- 搜速区 -->
    <div class="search">
      <el-button
        type="primary"
        icon="el-icon-document-add"
        @click="addClick"
      ></el-button>
    </div>
    <!-- 表格 -->
    <el-table :data="tableData" style="width: 99.9%" size="mini">
      <el-table-column label="类型编号" width="180">
        <template slot-scope="scope">
          {{ scope.row.roomTypeId }}
        </template>
      </el-table-column>
      <el-table-column label="类型名称" width="180">
        <template slot-scope="scope">
          {{ scope.row.roomTypeName }}
        </template>
      </el-table-column>
      <el-table-column label="床位数" width="180">
        <template slot-scope="scope">
          {{ scope.row.bedNum }}
        </template>
      </el-table-column>
      <el-table-column label="价格" width="180">
        <template slot-scope="scope">
          {{ scope.row.roomTypePrice }}
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="warning"
            @click="handleEdit(scope.$index, scope.row)"
            ><i class="el-icon-edit-outline"></i
          ></el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            ><i class="el-icon-delete"></i
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加抽屉 -->
    <el-drawer
      :title="isAdd ? '添加客房类型' : '修改客房类型'"
      :visible.sync="drawer"
      direction="rtl"
      :before-close="drawerClose"
      size="40%"
    >
      <div style="padding: 0 15px">
        <el-form
          size="mini"
          :model="ruleForm"
          status-icon
          :rules="rules"
          ref="ruleForm"
          label-width="75px"
          class="demo-ruleForm"
        >
          <el-form-item label="类型名称" prop="roomTypeName">
            <el-input
              type="text"
              v-model="ruleForm.roomTypeName"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="床位数" prop="bedNum">
            <el-input
              type="text"
              v-model.number="ruleForm.bedNum"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="价格" prop="roomTypePrice">
            <el-input
              type="text"
              v-model.number="ruleForm.roomTypePrice"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="描述" prop="typeDescription">
            <quill-editor
              v-model="ruleForm.typeDescription"
              ref="myQuillEditor"
              style="height: 250px; margin-bottom: 100px"
            >
            </quill-editor>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">{{
              isAdd ? "添加" : "修改"
            }}</el-button>
            <el-button @click="resetForm('ruleForm')">取消</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-drawer>
  </div>
</template>
<script>
export default {
  data() {
    // 验证
    var validateRoomTypeName = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入客房类型名称"));
      } else {
        callback();
      }
    };
    var validateBedNum = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入床位数"));
      } else if (!Number.isInteger(value)) {
        callback(new Error("请输入合法数字"));
      } else {
        callback();
      }
    };
    var validateRoomTypePrice = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输价格"));
      } else if (!Number.isInteger(value)) {
        callback(new Error("请输入合法数字"));
      } else {
        callback();
      }
    };
    return {
      // 表格数据
      tableData: [],
      // 是否做添加操作
      isAdd: true,
      // 是否打开抽屉
      drawer: false,
      //表单数据
      ruleForm: {
        // 类型编号
        roomTypeId: "",
        // 类型名称
        roomTypeName: "",
        // 床位数
        bedNum: "",
        // 房间类型价格
        roomTypePrice: "",
        // 类型描述
        typeDescription: "",
      },
      // 表单验证数据
      rules: {
        // 验证
        roomTypeName: [{ validator: validateRoomTypeName, trigger: "blur" }],
        bedNum: [{ validator: validateBedNum, trigger: "blur" }],
        roomTypePrice: [{ validator: validateRoomTypePrice, trigger: "blur" }],
      },
    };
  },
  created() {
    // 调用获取表格数据的方法
    this.getTableData();
  },
  methods: {
    // 获取表格数据
    async getTableData() {
      // 获取房间类型信息
      let res = await this.$get("/roomType/list");
      this.tableData = res.data;
    },
    async handleEdit(index, row) {
      // 表示这是修改操作
      this.isAdd = false;
      // 打开抽屉
      this.drawer = true;
      // 获取类型编号
      let roomTypeId = row.roomTypeId;
      this.roomTypeId = row.roomTypeId;
      // 根据角色编号,获取角色对象
      let res = await this.$get("/roomType/getOne", { roomTypeId });
      this.ruleForm = res.data;
    },
    async handleDelete(index, row) {
      await this.$con_f("是否确定删除?");
      let roomTypeId = row.roomTypeId;
      // 删除对应的角色信息
      let { success, message } = await this.$post("/roomType/delete", {
        roomTypeId,
      });
      if (success) {
        this.$message({
          showClose: true,
          message: message,
          type: "success",
          duration: 1500,
        });
        // 获取最新数据
        this.getTableData();
      } else {
        this.$message({
          showClose: true,
          message: message,
          type: "error",
          duration: 1500,
        });
      }
    },
    // 添加按钮点击方法
    addClick() {
      this.drawer = true;
    },
    searchClick() {
      this.getTableData();
    },
    // 关闭抽屉
    drawerClose(done) {
      // 清空表单数据
      this.resetForm("ruleForm");
      // 表单恢复成默认状态
      this.isAdd = true;
      done();
    },
    // 表单提交方法
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        // 判断表单数据是否验证成功
        if (valid) {
          if (this.isAdd) {
            // 执行添加功能
            let { success, message } = await this.$post(
              "/roomType/add",
              this.ruleForm
            );
            // 判断是否添加成功
            if (success) {
              this.$message({
                showClose: true,
                message: message,
                type: "success",
                duration: 1500,
              });
              // 清空表单
              this.resetForm("ruleForm");
              // 关闭抽屉
              this.drawer = false;
              // 获取最新数据
              this.getTableData();
            } else {
              this.$message({
                showClose: true,
                message: message,
                type: "error",
                duration: 1500,
              });
            }
          } else {
            // 执行修改操作
            let { success, message } = await this.$post(
              "/roomType/update",
              this.ruleForm
            );
            // 判断是否修改成功
            if (success) {
              this.$message({
                showClose: true,
                message: message,
                type: "success",
                duration: 1500,
              });
              this.drawer = false;
              // 获取最新数据
              this.getTableData();
            } else {
              this.$message({
                showClose: true,
                message: message,
                type: "error",
                duration: 1500,
              });
            }
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 表单重置方法
    resetForm(formName) {
      this.$refs[formName].resetFields();
      // 清空数据
      this.ruleForm = {
        // 类型编号
        roomTypeId: "",
        // 类型名称
        roomTypeName: "",
        // 床位数
        bedNum: "",
        // 房间类型价格
        roomTypePrice: "",
        // 房间类型图片
        roomTypePhoto: "",
        // 类型描述
        typeDescription: "",
      };
    },
  },
};
</script>
<style scoped lang='scss'>
.search {
  padding: 4px 10px;
}
.avatar-uploader {
  width: 150px;
  height: 150px;
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  justify-content: space-around;
  overflow: hidden;
}
.avatar-uploader:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 150px;
  height: 150px;
  line-height: 150px;
  text-align: center;
}
.avatar {
  width: 150px;
  height: 150px;
  max-width: 100%;
  max-height: 100%;
  display: block;
}
</style>